<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="../../pub/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>部门管理</title>
<%@include file="../../pub/resource.jsp"%>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/zTreeStyle/zTreeStyle.css" />
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.ztree.core-3.5.min.js"></script>
<style type="text/css">
body {
	padding-bottom: 40px;
}

.sidebar-nav {
	padding: 9px 0;
}

@media ( max-width : 980px) {
	/* Enable use of floated navbar text */
	.navbar-text.pull-right {
		float: none;
		padding-left: 5px;
		padding-right: 5px;
	}
}
</style>
<SCRIPT type="text/javascript">
<!--
	var setting = {};

	var zNodes = ${json};

	$(document)
			.ready(
					function() {
						$.fn.zTree.init($("#treeDemo"), setting, zNodes);
						$("#UpdateSelDepart").click(
								function() {
									var name = $("#selDepart").val();
									var id = $("#selId").val();
									if (name == "") {
										alertWin("必须输入新的部门名称！！！", "warning");
										return;
									}
									if (id == "") {
										alertWin("必须选定一个部门！！！", "warning");
										return;
									}
									$.ajax({
										url : "update.do",
										data : {
											name : name,
											id : id
										},
										type : "post",
										dataType : "text",
										success : function(result) {
											if (result == "success") {
												alertWinWithRefresh("修改成功！",
														"succeed");
											}
										}
									});
								});

						$("#addSelDepart").click(
								function() {
									var name = $("#addDepart").val();
									var id = $("#selId").val();
									if (name == "") {
										alertWin("必须输入新的部门名称！！！", "warning");
										return;
									}
									if (id == "") {
										alertWin("必须选定一个部门！！！", "warning");
										return;
									}
									$.ajax({
										url : "add.do",
										data : {
											name : name,
											parentId : id
										},
										type : "post",
										dataType : "text",
										success : function(result) {
											if (result == "success") {
												alertWinWithRefresh("添加成功！",
														"succeed");
											}
										}
									});
								});

						$("#delSelDepart")
								.click(
										function() {
											var id = $("#selId").val();
											if (id == "") {
												alertWin("请选中要删除的部门！！！",
														"warning");
												return;
											}
											if (confirm("你确定要删除选中的部门？")) {
												$
														.ajax({
															url : "del.do",
															type : "post",
															dataType : "text",
															data : {
																id : id
															},
															success : function(
																	data) {
																if (data == "success") {
																	alertWinWithRefresh(
																			"删除成功！",
																			"succeed");
																}
															}
														});
											}
										});
						$("#btnRole")
								.click(
										function() {
											var depId = $("#selId").val();
											if (depId == "") {
												alertWin("请选择部门！", "warning");
												return true;
											}
											$
													.ajax({
														url : "roles.do",
														type : "get",
														data : {

														},
														dataType : "json",
														success : function(data) {
															var content = "";
															for ( var i in data) {
																content += "<input type='checkbox' name='roles' value='"+data[i].id+"' class='checkbox'>"
																		+ data[i].name
																		+ "&nbsp;&nbsp;";
																if (i % 3 == 0
																		&& i != 0) {
																	content += "<br><br>";
																}
															}
															currentWin.$
																	.dialog({
																		title : "角色管理",
																		content : "<div>"
																				+ content
																				+ "</div>",
																		lock : true,
																		path : rootPath+"/css",
																		width : "300px",
																		init : getDepRoles,
																		ok : function() {
																			var selected = "";
																			currentWin
																					.$(
																							":checkbox[name='roles']:checked")
																					.each(
																							function(
																									index,
																									element) {
																								selected += $(
																										this)
																										.val()
																										+ ",";
																							});
																			if (selected == "") {
																				alertWin(
																						"请选择相应的角色！",
																						"warning");
																				return true;
																			}
																			$
																					.ajax({
																						url : "addDepRoles.do",
																						type : "post",
																						data : {
																							roles : selected,
																							depId : depId
																						},
																						dataType : "text",
																						success : function(
																								data) {
																							if (data == "success") {
																								alertWin(
																										"操作成功！",
																										"succeed");
																							}
																						}
																					})
																		},
																		okVal : "添加",
																		cancel : true,
																		cancelVal : "关闭"
																	});
														}
													})
										});
					});

	var getDepRoles = function() {
		currentWin.$(":checkbox[name='roles']").removeAttr("checked");
		var depId = $("#selId").val();
		if (depId == "") {
			alertWin("请选择部门！", "warning");
			return false;
		}
		$.ajax({
			url : "rolesByDepId.do",
			type : "post",
			data : {
				depId : depId
			},
			dataType : "text",
			success : function(data) {
				var strs = data.split(",");
				currentWin.$(":checkbox[name='roles']").each(function(index, ele) {
					for ( var i in strs) {
						if (strs[i] == $(this).val()) {
							$(this).attr("checked", "checked");
							break;
						}
					}
				});
			}
		});

	}

	var nodeClick = function(id, name, parentId) {
		$("#delSelDepart").removeAttr("disabled");
		if (parentId == 0) {
			$("#delSelDepart").attr("disabled", "disabled");
		}
		$("#selDepart").val(name);
		$("#selId").val(id);
	}
//-->
</SCRIPT>
</head>
<body>
	<div
		style="width: 300px; min-height: 600px; float: left; text-align: center; padding: 10px 0px 0px 20px;">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
	<div
		style="float: left; border-left: 2px dashed #C7BEB5; text-align: left; width: 600px; min-height: 600px;">
		<table width="99%" border="0" cellspacing="0" cellpadding="0"
			class="table table-bordered table-hover definewidth m10">
			<tr>
				<td style="text-align: right;">当前选择部门：</td>
				<td style="text-align: left;"><input type="text"
					name="selDepart" id="selDepart" placeholder="当前选择部门"
					class="abc input-default" /> <input type="hidden" name="selId"
					id="selId" /></td>
			</tr>
			<tr>
				<td style="text-align: right;">添加子部门：</td>
				<td style="text-align: left;"><input type="text"
					name="addDepart" id="addDepart" placeholder="要添加的子部门名称"
					class="abc input-default"></td>
			</tr>
			<tr>
				<td colspan="2"><shiro:hasPermission name="部门管理:删除">
						<input type="button" value="删除当前选择部门" id="delSelDepart"
							name="delSelDepart" class="btn btn-danger" />
					</shiro:hasPermission> <shiro:hasPermission name="部门管理:修改">
				&nbsp;&nbsp;<input type="button" value="修改当前选择部门"
							id="UpdateSelDepart" name="UpdateSelDepart"
							class="btn btn-warning" />
					</shiro:hasPermission> <shiro:hasPermission name="部门管理:添加">
				&nbsp;&nbsp;<input type="button" value="添加子部门" id="addSelDepart"
							name="addSelDepart" class="btn btn-success" />
					</shiro:hasPermission> &nbsp;&nbsp;<input type="button" value="角色管理" id="btnRole"
					name="btnRole" class="btn btn-info" style="display: none;" /></td>
			</tr>
		</table>
	</div>
</body>
</html>